﻿import QtQuick 2.12
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

ColumnLayout{

    Layout.fillWidth: true
    Layout.fillHeight: true
    spacing: 10

    property int offset: 0

    Rectangle{
        width: parent.width
        height: 30
        color: "#00000000"
        Text{
            x:10
            verticalAlignment: Text.AlignBottom
            height: parent.height
            text: qsTr("搜索音乐")
            font.family: window.mFONT_FAMILY
            font.pointSize: 15
            color:"white"
        }
    }
    RowLayout{

        Layout.fillWidth: true

        TextField{
            id:searchInput
            font.family: window.mFONT_FAMILY
            font.pointSize: 16
            selectByMouse: true
            selectionColor: "#999999"
            placeholderText: qsTr("请输入搜索关键词")
            color:"#eeffffff"
            background: Rectangle{
                color:"#00000000"
                border.color: "#B2B2B2"
                border.width: 2
                radius: 4
                opacity: 0.05
                implicitHeight: 40
                implicitWidth: 400
            }
            focus: true
            Keys.onPressed: if(event.key ===Qt.Key_Enter || event.key === Qt.Key_Return)doSearch()

        }
        MusicIconButton{
            iconSource: "qrc:/images/search"
            toolTip: "搜索"
            onClicked: doSearch()
        }
    }
    MusicListView{
        deleteble: false
        id:musiclistView
        onLoadMore: doSearch(offset,current)
        Layout.topMargin: 10
    }
    function doSearch(offset = 0,current = 0){
        loading.open()

        var keywords = searchInput.text
        if(keywords.length <1)return
        function onReply(reply){
            loading.close()
            http.onReplySignal.disconnect(onReply)
            try{
                if(reply.length <1){
                    notification.openError("搜索结果为空！")

                    return
                }

            var result = JSON.parse(reply).result
            musiclistView.current = current
            musiclistView.all = result.songCount
            musiclistView.musiclist = result.songs.map(item=>{
                                                       return{
                                                           id:item.id,
                                                           name:item.name,
                                                           artist:item.artists[0].name,
                                                           album:item.album.name,
                                                               cover:""
                                                       }

                                                   })


            }catch(err){
                notification.openError("搜索结果出错！")

            }

        }
        http.onReplySignal.connect(onReply)
        http.connect("search?keywords="+keywords +"&offset="+offset+"&limit=60")
    }
}



